package cn.wanho.service.impl;

import cn.wanho.dto.CmsChooseTopic;
import cn.wanho.dto.CmsExam;
import cn.wanho.listener.DataListener;
import cn.wanho.listener.DataListenerA;
import cn.wanho.listener.DataListenerB;
import cn.wanho.listener.DataListenerD;
import cn.wanho.rvo.ResultEnum;
import cn.wanho.rvo.ResultVO;
import cn.wanho.rvo.myeasyexcel.CmsMultileChoiceQuestionData;
import cn.wanho.rvo.myeasyexcel.CmsSummaryData;
import cn.wanho.rvo.myeasyexcel.CmsTextProgrammerData;
import cn.wanho.rvo.myeasyexcel.CmsTrueFalseData;
import cn.wanho.service.*;
import com.alibaba.excel.EasyExcel;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class PaperServiceImpl implements PaperService {
    @Resource
    private CmsChoiceService cmsChoiceService;
    @Resource
    private CmsSummaryService cmsSummaryService;
    @Resource
    private CmsTrueFalseService cmsTrueFalseService;
    @Resource
    private CmsTextProgrammerService cmsTextProgrammerService;
    @Resource
    private CmsExamService cmsExamService;
    @Resource
    private FillQuestionService fillQuestionService;
    @Override
    public ResultVO createPaper(MultipartFile file, String examName,String startTime,Integer keepTime) throws ParseException {
        Date startDate = DateUtils.parseDate(startTime, new String[]{"yyyy-MM-dd HH:mm:ss"});
        Date endDate = DateUtils.addMinutes(startDate, keepTime);
        System.out.println(startDate);
        System.out.println(endDate);
        CmsExam cmsExam = new CmsExam();
        cmsExam.setStageExamName(examName);
        cmsExam.setStartTime(startDate);
        cmsExam.setEndTime(endDate);
        cmsExamService.insert(cmsExam);
        Integer id = cmsExam.getId();
        try {
            EasyExcel.read(file.getInputStream(), CmsMultileChoiceQuestionData.class, new DataListener(cmsChoiceService,id)).sheet(0).doRead();
            EasyExcel.read(file.getInputStream(), CmsSummaryData.class, new DataListenerA(cmsSummaryService,id)).sheet(1).doRead();
            EasyExcel.read(file.getInputStream(), CmsTrueFalseData.class, new DataListenerB(cmsTrueFalseService,id)).sheet(2).doRead();
            EasyExcel.read(file.getInputStream(), CmsTextProgrammerData.class, new DataListenerD(cmsTextProgrammerService,id)).sheet(3).doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultVO.success(ResultEnum.TEST_CREATE_SUCCESS.getResCode(),ResultEnum.TEST_CREATE_SUCCESS.getResMsg() );
    }

     @Override
     public ResultVO fillQuestion(Integer paperId, List<Integer> choiceList, List<Integer> summaryList, List<Integer> judgeList, List<Integer> otherList){
        Integer sort=1;
         for (Integer integer : choiceList) {
             CmsChooseTopic cmsChooseTopic=new CmsChooseTopic();
             cmsChooseTopic.setSort(sort);
             cmsChooseTopic.setExamId(paperId);
             cmsChooseTopic.setChoiceId(integer);
             fillQuestionService.insert(cmsChooseTopic);
             sort++;
         }
         for (Integer integer : summaryList) {
             CmsChooseTopic cmsChooseTopic=new CmsChooseTopic();
             cmsChooseTopic.setSort(sort);
             cmsChooseTopic.setExamId(paperId);
             cmsChooseTopic.setSummaryId(integer);
             fillQuestionService.insert(cmsChooseTopic);
             sort++;
         }
         for (Integer integer : judgeList) {
             CmsChooseTopic cmsChooseTopic=new CmsChooseTopic();
             cmsChooseTopic.setSort(sort);
             cmsChooseTopic.setExamId(paperId);
             cmsChooseTopic.setTrueFalseId(integer);
             fillQuestionService.insert(cmsChooseTopic);
             sort++;
         }
         for (Integer integer :otherList) {
             CmsChooseTopic cmsChooseTopic=new CmsChooseTopic();
             cmsChooseTopic.setSort(sort);
             cmsChooseTopic.setExamId(paperId);
             cmsChooseTopic.setTextProgrammerId(integer);
             fillQuestionService.insert(cmsChooseTopic);
             sort++;
         }
         return ResultVO.success(ResultEnum.ADD_QUESTIONS_SUCCESS.getResCode(),ResultEnum.ADD_QUESTIONS_SUCCESS.getResMsg() );
     }
}
